package com.auth.control;

import com.alibaba.fastjson.JSON;
import com.auth.domain.module.po.base.BasePo;
import com.common.global.R;
import com.language.util.MessageUtil;
import com.workflow.sdk.core.WorkflowExecutor;
import com.workflow.sdk.core.WorkflowExecutorResult;
import com.workflow.sdk.domain.model.idenfifer.WorkflowId;
import com.workflow.sdk.domain.service.Workflow;
import com.workflow.sdk.domain.model.entity.dto.WorkflowerExecuteParamDto;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.core.NodeComponent;
import com.yomahub.liteflow.flow.LiteflowResponse;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.RandomStringUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.DeferredResult;

import javax.annotation.Resource;

@RestController
@RequestMapping("/test")
@Slf4j
public class TestControl {

    @Autowired
    private Workflow workflow;

    @Component
    public class WorkflowExecute1 implements WorkflowExecutor<BasePo> {

        @Override
        public WorkflowExecutorResult execute(BasePo params, String remark) {
            System.out.println(remark);
            return null;
        }

        @Override
        public String getTag() {
            return String.format(TAG, "test1", 1);
        }
    }

    @Component
    public class WorkflowExecute2 implements WorkflowExecutor<String> {

        @Override
        public WorkflowExecutorResult execute(String params, String remark) {
            System.out.println(remark);
            return new WorkflowExecutorResult(params);
        }

        @Override
        public String getTag() {
            return String.format(TAG, "test1", 2);
        }
    }

    @Component
    public class WorkflowExecute3 implements WorkflowExecutor<BasePo> {

        @Override
        public WorkflowExecutorResult execute(BasePo params, String remark) {
            System.out.println(remark);
            return null;
        }

        @Override
        public String getTag() {
            return String.format(TAG, "test1", 3);
        }
    }

    @Component
    public class WorkflowExecute4 implements WorkflowExecutor<BasePo> {

        @Override
        public WorkflowExecutorResult execute(BasePo params, String remark) {
            System.out.println(remark);
            return null;
        }

        @Override
        public String getTag() {
            return String.format(TAG, "test1", 4);
        }
    }

    @GetMapping("/get1")
    public DeferredResult<String> getTest() throws InterruptedException {
        DeferredResult<String> response = new DeferredResult<String>(10000L, null);
        response.onCompletion(new Runnable() {

            @SneakyThrows
            @Override
            public void run() {
                System.out.println("OKOKO");
                // 请求处理完成后所做的一些工作
                Thread.sleep(5000);
            }
        });
        // 设置响应结果
        // 调用此方法时立即向浏览器发出响应；未调用时请求被挂起
        response.setResult("OKOKOKO");

        return response;
    }

    @GetMapping("/get2")
    public void getTest2(@RequestParam(value = "tableName", required = false) String tableName) throws InterruptedException {
        Long workflowExecuteId = workflow.createWorkflowExecuteWithTable(new WorkflowId(1L), RandomStringUtils.random(8, "abcdefghijklmnopqrstuvwxyz0123456789"), tableName);

        workflow.workflowExecuteWithTable(new WorkflowerExecuteParamDto(workflowExecuteId, "我提交审核", null), tableName);
        workflow.workflowExecuteWithTable(new WorkflowerExecuteParamDto(workflowExecuteId, "审核选择打印流程1", "3"), tableName);
        workflow.workflowExecuteWithTable(new WorkflowerExecuteParamDto(workflowExecuteId, "执行打印方法1", null), tableName);
        //workflower.workflowExecute(workflowExecuteId, "我打印4", null);

    }

    @GetMapping("/get3")
    public R<String> getTest3() throws InterruptedException {

        return R.success(MessageUtil.message("error.1"));
    }





    @Component("a")
    public class ACmp extends NodeComponent {

        @Override
        public void process() {
            System.out.println("a run！");
        }
    }

    @Component("b")
    public class BCmp extends NodeComponent {

        @Override
        public void process() {
            System.out.println("b run！");
        }
    }

    @Component("c")
    public class CCmp extends NodeComponent {

        @Override
        public void process() {
            System.out.println("c run！");
        }
    }

    @Resource
    private FlowExecutor flowExecutor;

    @GetMapping("/get4")
    public void test4() {
        LiteflowResponse response = flowExecutor.execute2Resp("chain1", "arg");
        System.out.println(JSON.toJSONString(response));
    }

}
